package com.tx.admin.util.common;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by ace on 2017/9/27.
 */
public class UUIDUtils {

	private static final List<Character> list = new ArrayList<>();
	private static final Integer length = 4;
	private static final SimpleDateFormat format =new SimpleDateFormat("yyyyMMddhhmmss");

	static {
		
		for(int x = 'a';x<'z';x++) {
			list.add((char)x);
		}
		for(int x = 'A';x<'Z';x++) {
			list.add((char)x);
		}	for(int x = '0';x<'9';x++) {
			list.add((char)x);
		}
	}
	
	public static Integer getRandom(int min,int max) {
		int random = (int) (Math.random()*(max-min));
		return random+min;
	}
	
	/**
	 * 生成唯一Id
	 * @param clazz
	 * @return
	 */
	public static final String getId(Class<?> clazz) {
		StringBuffer sb = new StringBuffer();
		String date = format.format(new Date());
		sb.append(date);
		for(int x = 0 ;x < length ;x++) {
			char ch = list.get(getRandom(0,list.size()));
			sb.append(ch);
		}
		return sb.toString();
	}
	
	public static final String getId() {
		StringBuffer sb = new StringBuffer();
		String date = format.format(new Date());
		sb.append(date);
		for(int x = 0 ;x < length ;x++) {
			char ch = list.get(getRandom(0,list.size()));
			sb.append(ch);
		}
		return sb.toString();
	}
	
	/**
	 * 根据唯一字符串，生成唯一数字字符串
	 * @param id
	 * @return
	 */
	public static int map(String id) {
		return id.hashCode();
	}
}
